<template>
	<el-dialog
		:title="title"
		v-loading="loading"
		v-model="props.params.visible"
		width="1100px"
		append-to-body
		:before-close="cancel"
	>
		<el-form
			:model="addForm"
			ref="addRef"
			:inline="true"
			:rules="addRules"
			label-width="120px"
		>
			<el-form-item label="区域号" prop="areaNo">
				<el-input
					v-model="addForm.areaNo"
					placeholder="请输入"
					clearable
					style="width: 360px"
				/>
			</el-form-item>

			<el-form-item label="层号" prop="layerNo">
				<el-input
					v-model="addForm.layerNo"
					placeholder="请输入"
					clearable
					style="width: 360px"
				/>
			</el-form-item>
			<el-form-item label="货架号" prop="shelfNo">
				<el-input
					v-model="addForm.shelfNo"
					placeholder="请输入"
					clearable
					style="width: 360px"
				/>
			</el-form-item>

			<el-form-item label="库位类型" prop="shelfStatus">
				<el-select
					v-model="addForm.shelfStatus"
					clearable
					placeholder="请选择库位类型"
					style="width: 360px"
				>
					<el-option
						v-for="item in location_status"
						:key="item.value"
						:label="item.label"
						:value="item.value"
					/>
				</el-select>
			</el-form-item>

			<!-- <el-form-item label="是否存满" prop="status">
				<el-input
					v-model="addForm.status"
					placeholder="请输入"
					clearable
          style="width: 360px;"
				/>
			</el-form-item> -->

			<el-form-item label="已存放的数量" prop="storageQty">
				<el-input-number
					:min="0"
					v-model="addForm.storageQty"
					placeholder="请输入"
					clearable
					style="width: 360px"
				/>
			</el-form-item>
			<el-form-item label="可存放的数量	" prop="sumQty">
				<el-input-number
					:min="0"
					v-model="addForm.sumQty"
					placeholder="请输入"
					clearable
					style="width: 360px"
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup>
import {
	getWarehouseLocationById,
	editWarehouseLocation,
	addWarehouseLocation,
} from "@/api/skesWarehouseLocation"
import { userOptionSelect } from "@/api/system/user"
import { listData } from "@/api/system/dict/data"

import { computed, watchEffect } from "vue"
const props = defineProps({
	params: Object,
	visible: Boolean,
	options: Array,
})
const emits = defineEmits(["update:visible", "refresh"])
const { proxy } = getCurrentInstance()
const { location_status } = proxy.useDict("location_status")
const title = computed(() =>
	props.params.type === "add" ? "库位新增" : "库位修改"
)
const loading = ref(false)
const addForm = ref({
	areaNo: "", //	区域号	string	body
	layerNo: "", //	层号	string	body
	shelfNo: "", //	货架号	string	body
	status: "", //	是否存满	integer	body
	storageQty: "", //	已存放的数量	integer	body
	sumQty: "", //可存放的数量
	shelfStatus: "",
})
const addRules = ref({
	areaNo: [{ required: true, message: "", trigger: "blur" }],
	layerNo: [{ required: true, message: "", trigger: "blur" }],
	shelfNo: [{ required: true, message: "", trigger: "blur" }],
	status: [{ required: true, message: "", trigger: "blur" }],
	storageQty: [{ required: true, message: "", trigger: "blur" }],
	sumQty: [{ required: true, message: "", trigger: "blur" }],
	shelfStatus: [{ required: true, message: "", trigger: "blur" }],
})
const shortcuts = [
	{
		text: "今天",
		value: new Date(),
	},
	{
		text: "昨天",
		value: () => {
			const date = new Date()
			date.setTime(date.getTime() - 3600 * 1000 * 24)
			return date
		},
	},
	{
		text: "一周前",
		value: () => {
			const date = new Date()
			date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
			return date
		},
	},
]
const isUpdate = ref(false)
watchEffect(() => {
	const { visible, type } = props.params
	isUpdate.value = type === "add" ? true : false
	if (visible && type !== "add") getDetail()
})

function getDetail() {
	loading.value = true
	getWarehouseLocationById(props.params.id).then((response) => {
		const keys = [
			"id",
			"areaNo",
			"layerNo",
			"num",
			"shelfNo",
			"status",
			"storageQty",
			"sumQty",
		]

		addForm.value = {}

		keys.forEach((key) => {
			addForm.value[key] = response.data[key]
		})
		loading.value = false
	})
}
/** 重置表单  */
function reset() {
	proxy.resetForm("addRef")
}
/** 提交按钮 */
function submitForm() {
	proxy.$refs["addRef"].validate((valid) => {
		if (valid) {
			if (props.params.type === "add") {
				addWarehouseLocation(addForm.value).then((response) => {
					proxy.$modal.msgSuccess("新增库位成功")
					emits("refresh")
					cancel()
				})
			} else {
				editWarehouseLocation(addForm.value).then((response) => {
					proxy.$modal.msgSuccess("修改库位成功")
					emits("refresh")
					cancel()
				})
			}
		}
	})
}
/** 取消按钮 */
function cancel() {
	reset()
	emits("update:visible", false)
}
</script>
